/*
 * @Author: szx
 * @Date: 2022-03-02 13:04:25
 * @LastEditTime: 2022-03-02 13:21:51
 * @Description:
 * @FilePath: \leetcode\400-499\415\415.js
 */
/**
 * @param {string} num1
 * @param {string} num2
 * @return {string}
 */
var addStrings = function (num1, num2) {
    let n1 = num1.length - 1;
    let n2 = num2.length - 1;
    const arr = [];
    let add = 0;
    while (n1 >= 0 || n2 >= 0) {
        const x = n1 >= 0 ? num1[n1] - '0' : 0;
        const y = n2 >= 0 ? num2[n2] - '0' : 0;
        const res = x + y + add;
        arr.push(res % 10);
        add = Math.floor(res / 10);
        n1--;
        n2--;
    }
    if (add == 1) arr.push(1);
    return arr.reverse().join('');
};
console.log(addStrings('123', '999'));
